package kz.myproject.server;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import kz.myproject.client.GreetingService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Server side greeting service implementation as a Spring bean named
 * greetingService.
 */
@Service("greetingService")
public class GreetingServiceImpl implements GreetingService {

	@PersistenceContext(name = "MyPersistenceUnit")
	private EntityManager entityManager;

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String phrase() {
		String sql = "select data from entity";
		Query query = entityManager.createNativeQuery(sql);
		List list = query.getResultList();
		StringBuilder stringBuilder = new StringBuilder();
		for (Object obj : list) {
			String str=(String)obj;
			stringBuilder.append(str);
			stringBuilder.append("; ");
		}
		String result = stringBuilder.toString();
		if (result.length() > 0 && result.charAt(result.length() - 2) == ';') {
			result = result.substring(0, result.length() - 2);
		}
		return result.toString();
	}
}
